<template>
  <div>
    <el-form :model="data" :rules="rules" ref="form" label-position="left" label-width="30%" class="ml30">
      <el-row v-if="roles.indexOf('admin') !== -1">
        <el-col :span="12">
          <el-form-item label="省份：" prop="province">
            <el-select class="width80" v-model="data.provinceId" placeholder="请选择省份" filterable>
              <el-option
                v-for="item in province"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="城市：" prop="city">
            <el-select class="width80" v-model="data.cityId" placeholder="请选择城市" filterable>
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="区：" prop="area">
            <el-select class="width80" v-model="data.areaId" placeholder="请选择区" filterable>
              <el-option
                v-for="item in area"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="roles.indexOf('admin') !== -1">
          <el-form-item label="试验区：" prop="experimentArea">
            <el-select class="width80" v-model="data.experimentAreaId" placeholder="请选择试验区" filterable>
              <el-option
                v-for="item in area"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学校名称：" prop="name">
            <el-input class="width80" v-model="data.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否示范学校：" prop="isStandout">
            <el-select class="width80" v-model="data.isStandout" placeholder="是否示范学校">
              <el-option label="是" :value="true"/>
              <el-option label="否" :value="false"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学制：" prop="schoolType">
            <el-select class="width80" v-model="data.schoolType" placeholder="学制">
              <el-option label="小学" value="小学"/>
              <el-option label="初中" value="初中"/>
              <el-option label="高中" value="高中"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校长姓名：" prop="headmaster">
            <el-input class="width80" v-model="data.headmaster"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="校长电话：" prop="headmasterPhone">
            <el-input class="width80" v-model="data.headmasterPhone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校长邮箱：" prop="headmasterEmail">
            <el-input class="width80" v-model="data.headmasterEmail"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系人姓名：" prop="contact">
            <el-input class="width80" v-model="data.contact"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人电话：" prop="contactPhone">
            <el-input class="width80" v-model="data.contactPhone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系人邮箱：" prop="contactEmail">
            <el-input class="width80" v-model="data.contactEmail"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="加入时间：" prop="joinAt">
            <el-date-picker v-model="data.joinAt" type="date" class="width80" placeholder="选择加入时间"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学生数量：" prop="studentNum">
            <el-input-number class="width80" v-model="data.studentNum" :min="0"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学校编码：" prop="schoolCode">
            <el-input class="width80" v-model="data.schoolCode"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="编码：" prop="code">
            <el-input class="width80" v-model="data.code"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否有申请表：" prop="hasApply">
            <el-select class="width80" v-model="data.hasApply" placeholder="是否有申请表">
              <el-option label="是" :value="true"/>
              <el-option label="否" :value="false"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学校主页：" prop="homepage">
            <el-input class="width80" v-model="data.homepage"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学校公众号：" prop="officialAccounts">
            <el-input class="width80" v-model="data.officialAccounts"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否授予实验校牌匾：" prop="isExperiment">
            <el-select class="width80" v-model="data.isExperiment" placeholder="是否授予实验校牌匾">
              <el-option label="是" :value="true"/>
              <el-option label="否" :value="false"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="授予实验校牌匾时间：" prop="experimentAt">
            <el-date-picker v-model="data.experimentAt" type="date" class="width80" placeholder="选择授予实验校牌匾时间"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="授予示范校牌匾时间：" prop="standoutAt">
            <el-date-picker v-model="data.standoutAt" type="date" class="width80" placeholder="选择授予示范校牌匾时间"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系地址：" prop="contactAddress">
            <el-input class="width80" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="data.contactAddress"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer">
      <el-button class="el-button--blue" type="primary" @click="submitSchoolAdd">保 存</el-button>
      <el-button @click="msg.visible=false">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { GetProvinceList, GetCityListByProvinceId, GetAreaListByCityId, GetAreaList } from '../../../api/select'
import { awaitCheckUserName, awaitCheckSchoolCode, SubmitSchoolAdd } from '../../../api/form'
import { mapGetters } from 'vuex'
export default {
  name: 'school-add',
  model: {
    prop: 'msg',
    event: 'cc'
  },
  props: {
    msg: {}
  },
  data () {
    let validatePhone = (rule, value, callback) => {
      if (typeof value !== 'string') {
        value = ''
      }
      if (value.toString() !== '') {
        let res = awaitCheckUserName(value, 'all', 0)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    let validateSchoolCode = (rule, value, callback) => {
      if (value.toString() !== '') {
        let res = awaitCheckSchoolCode(this.data.schoolCode, 0)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    return {
      province: [],
      city: [],
      area: [],
      data: {
        provinceId: 11,
        cityId: 1101,
        areaId: '',
        experimentAreaId: '',
        name: '',
        isStandout: false,
        schoolType: '',
        headmaster: '',
        headmasterPhone: '',
        joinAt: '',
        studentNum: 0,
        schoolCode: '',
        code: '',
        hasApply: false,
        homepage: '',
        officialAccounts: '',
        isExperiment: false,
        experimentAt: '',
        standoutAt: '',
        contactAddress: ''
      },
      rules: {
        name: [
          { required: true, message: '学校名称不能为空', trigger: 'change' },
          { min: 1, max: 20, message: '长度在 20 个字符以内', trigger: 'change' }
        ],
        isStandout: [{ type: 'boolean', message: '请选择是否授权', trigger: 'change' }],
        schoolType: [
          { required: true, message: '学制不能为空', trigger: 'change' },
          { type: 'string', pattern: /^(小学)|(初中)|(高中)$/, message: '非法学制', trigger: 'change' }
        ],
        headmaster: [{ min: 0, max: 10, message: '长度在 10 个字符以内', trigger: 'change' }],
        headmasterPhone: [{ type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'change' }],
        headmasterEmail: [{ type: 'string', pattern: /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/, message: '非法邮箱', trigger: 'change' }],
        contact: [{ min: 0, max: 10, message: '长度在 10 个字符以内', trigger: 'change' }],
        contactPhone: [
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'change' },
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        contactEmail: [{ type: 'string', pattern: /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/, message: '非法邮箱', trigger: 'change' }],
        schoolCode: [
          { required: true, message: '学校编码不能为空', trigger: 'change' },
          { min: 1, max: 50, message: '长度在 50 个字符以内', trigger: 'change' },
          { validator: validateSchoolCode, trigger: 'blur' }
        ],
        code: [{ min: 0, max: 20, message: '长度在 20 个字符以内', trigger: 'change' }],
        hasApply: [{ type: 'boolean', message: '请选择是否有申请表', trigger: 'change' }],
        homepage: [{ min: 0, max: 50, message: '长度在 50 个字符以内', trigger: 'change' }],
        officialAccounts: [{ min: 0, max: 50, message: '长度在 50 个字符以内', trigger: 'change' }],
        isExperiment: [{ type: 'boolean', message: '请选择是否授权', trigger: 'change' }],
        contactAddress: [{ min: 0, max: 255, message: '长度在 255 个字符以内', trigger: 'change' }]
      }
    }
  },
  mounted () {
    if (this.roles.indexOf('admin') !== -1) {
      GetProvinceList().then(res => {
        this.province = res.data.province
        this.data.provinceId = res.data.province[0].id
      })
    } else if (this.roles.indexOf('area') !== -1) {
      GetAreaList().then(res => {
        this.area = res.data.area
        this.data.areaId = res.data.area[0].id
      })
    }
  },
  methods: {
    submitSchoolAdd () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          SubmitSchoolAdd(this.data).then(res => {
            this.msg.visible = false
            this.$store.dispatch('SetFlash')
          })
        } else {
          return false
        }
      })
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  watch: {
    'data.provinceId': {
      handler () {
        if (this.roles.indexOf('admin') !== -1) {
          GetCityListByProvinceId(this.data.provinceId).then(res => {
            this.city = res.data.city
            this.data.cityId = res.data.city[0].id
          })
        }
      },
      immediate: true,
      deep: true
    },
    'data.cityId': {
      handler () {
        if (this.roles.indexOf('admin') !== -1) {
          GetAreaListByCityId(this.data.cityId).then(res => {
            this.area = res.data.area
            this.data.areaId = res.data.area[0].id
            this.data.experimentAreaId = res.data.area[0].id
          })
        }
      },
      immediate: true,
      deep: true
    }
  }
}
</script>

<style scoped>
  .width80 {
    width: 80%;
  }
  .footer {
    display: flex;
    justify-content: flex-end;
  }
</style>
